package com.auoem.radio.ui.radio;

import android.os.Bundle;
import android.view.View;

import androidx.annotation.Nullable;
import androidx.appcompat.app.ActionBar;
import androidx.recyclerview.widget.LinearLayoutManager;

import com.auctus.am.developer.AuHelper;
import com.auctus.am.framework.entity.Contact;
import com.auctus.am.framework.enums.ApiStatusEnum;
import com.auctus.am.framework.enums.CallTypeEnums;
import com.auctus.am.framework.imp.RadioManager;
import com.auctus.am.framework.interfaces.ApiCallback;
import com.auctus.am.framework.utils.LogM;
import com.auoem.radio.R;
import com.auoem.radio.databinding.ActivityCtListBinding;
import com.auoem.radio.base.BaseActivity;
import com.auoem.radio.common.adapter.BindingAdapter;
import com.auoem.radio.common.custview.CustomTitlebar;
import com.auoem.radio.common.custview.EditCtDialogFragment;


import java.util.List;
import java.util.function.IntConsumer;

public class CtListActivity extends BaseActivity implements IntConsumer, View.OnClickListener {

    private static final String TAG = "CtListActivity";
    ActivityCtListBinding mBinding;
    private List<Contact> contactList;
    private BindingAdapter<Contact> ctAdapter;

    EditCtDialogFragment editCtDialogFragment;

    Contact curContact;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mBinding = ActivityCtListBinding.inflate(getLayoutInflater());

        initActionBar();
        initData();
        setContentView(mBinding.getRoot());
    }

    private void initActionBar() {
        ActionBar actionBar = getSupportActionBar();
        assert actionBar != null;
        actionBar.hide();
        mBinding.ctCtList.setButtonOnClickCallback(new CustomTitlebar.ButtonCallback() {
            @Override
            public void OnClickResultCallback(CustomTitlebar.ButtonType btnType, View view) {
                switch (btnType) {
                    case LeftimgBtn:
                        finish();
                        break;
                }
            }
        });
    }

    private void initData() {
        contactList = RadioManager.getInstance().getRadioData().getContactList();
//        makeTestData();
        ctAdapter = new BindingAdapter<>(this, contactList, R.layout.adapter_item_ct);
        ctAdapter.setOnItemClickListener(this);
        mBinding.lvCt.setLayoutManager(new LinearLayoutManager(this));
        mBinding.lvCt.setAdapter(ctAdapter);
        LogM.d("ChFragment", "initData: " + contactList.size());
    }

    private void makeTestData() {
        Contact contact1 = new Contact();
        contact1.name = "name1";
        contact1.calllType = CallTypeEnums.GRP;
        contact1.cid = 2377;
        contactList.add(contact1);
        try {
            Thread.sleep(100);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        Contact contact2 = new Contact();
        contact2.name = "name2";
        contact2.calllType = CallTypeEnums.IND;
        contact2.cid = 1377;
        contactList.add(contact2);
    }

    @Override
    public void accept(int o) {
        curContact = contactList.get(o);
        showEditDialog(true);
    }

    @Override
    public void onClick(View v) {
        if (v == mBinding.vBottom || v == mBinding.btAdd) {
            add();
        }
    }

    private void showEditDialog(boolean isEdit) {
        if (editCtDialogFragment == null) {
            editCtDialogFragment = new EditCtDialogFragment();
            editCtDialogFragment.setOnEditFinishListener(new EditCtDialogFragment.OnEditFinishListener() {
                @Override
                public void onFinished(int opt, Contact contact) {
                    switch (opt) {
                        case 1:
                            edit(contact);
                            break;
                        case 2:
                            if (curContact == null) {
                                return;
                            }
                            del();
                            break;
                    }
                }
            });
        }
        if (!editCtDialogFragment.isAdded() && null == getSupportFragmentManager().findFragmentByTag("editDialog")) {
            editCtDialogFragment.showNow(getSupportFragmentManager(), "editDialog");
        }
        editCtDialogFragment.setContent(isEdit ? curContact : new Contact());
    }

    private void add() {
        if (RadioManager.getInstance().getRadioData().tooMuchCt()) {
            toastResultUI(R.string.hl_ct_toast_too_much);
            return;
        }
        curContact = null;
        showEditDialog(false);
    }

    private void edit(Contact contactNew) {
        LogM.d(TAG, "edit Contact: " + contactNew.toString());
        if (!contactNew.isValid()) {
            toastResultUI(R.string.hl_ct_toast_ct_input_invalid);
            return;
        }
        if (curContact == null) {
            if (contactNew.calllType == CallTypeEnums.ALL && RadioManager.getInstance().getRadioData().hasAllCall()) {
                toastResultUI(R.string.hl_ct_toast_all_call_one);
                return;
            }
            if (RadioManager.getInstance().getRadioData().editConflict(contactNew, true)) {
                toastResultUI(R.string.hl_ct_toast_ct_add_conflict);
                return;
            }
            RadioManager.getInstance().getRadioData().addContact(contactNew);
        } else {
            if (RadioManager.getInstance().getRadioData().editConflict(contactNew, false)) {
                toastResultUI(R.string.hl_ct_toast_ct_edit_conflict);
                return;
            }
            RadioManager.getInstance().getRadioData().updateContact(contactNew);
        }
        ctAdapter.notifyDataSetChanged();
        doOnEditCt(contactNew);
    }

    private void del() {
        if (!RadioManager.getInstance().getRadioData().canDelContact()) {
            toastResultUI(R.string.hl_ct_toast_all_call_one);
            return;
        }
        RadioManager.getInstance().getRadioData().delContact(curContact.tag);
        ctAdapter.notifyDataSetChanged();
    }

    private void doOnEditCt(Contact contact) {
        if (RadioManager.getInstance().getRadioData().getCurChannel() == null || !RadioManager.getInstance().getRadioData().getCurChannel().isDigModel) {
            return;
        }
        if (contact.calllType == CallTypeEnums.GRP) {
            showProgress(R.string.am_common_exe_ing);
            AuHelper.getInstance().getApi().setGroupCallAry(RadioManager.getInstance().getRadioData().getCtGrpRev(), true, new ApiCallback() {
                @Override
                public void onResponse(ApiStatusEnum status, Object result, String msg) {
                    toastResultUI(status);
                }
            });
        }
    }
}
